#include "key.h"
#include "timer.h"
#include "delay.h" 
#include "hx6203sys.h"

//extern u16 Power_List[Power_List_Len];  //功率预设数组
//extern u16 Power_List_Level;            //预设功率等级
//extern uint8_t P_SetFlag;               //设定功率标志
//extern uint8_t ACT_Count;               //动作计数


void KEY_EXTIX_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	NVIC_InitTypeDef  NVIC_InitStructure;
	EXTI_InitTypeDef  EXTI_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOG, ENABLE);//使能GPIOD,G

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //KEY0 对应引脚
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIO
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //KEY3 对应引脚
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIO
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //KEY2 对应引脚
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化GPIO
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //KEY1 对应引脚
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化GPIO
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG时钟
	
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource7);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource6);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOG, EXTI_PinSource15);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOG, EXTI_PinSource8);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line7|EXTI_Line6|EXTI_Line15|EXTI_Line8;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE
	EXTI_Init(&EXTI_InitStructure);//配置

	NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中断5-9使用同一个通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
	NVIC_Init(&NVIC_InitStructure);//配置
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;//外部中断10-15使用同一个通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
	NVIC_Init(&NVIC_InitStructure);//配置
    
    //初始化定时器2，用于检测按键
    TIM2_Int_Init(200, 7200);   //20ms
} 

void KEY_EXTIX_Enable(void){
	EXTI_InitTypeDef   EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line7|EXTI_Line6|EXTI_Line15|EXTI_Line8;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE
	EXTI_Init(&EXTI_InitStructure);//配置
}

void KEY_EXTIX_Disable(void){
	EXTI_InitTypeDef   EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line7|EXTI_Line6|EXTI_Line15|EXTI_Line8;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 
	EXTI_InitStructure.EXTI_LineCmd = DISABLE;//使能LINE
	EXTI_Init(&EXTI_InitStructure);//配置
}

/***********************新按键检测***************************
触发按键中断后开启定时器2进行检测，判断短按或长按
*************************************************************/
void EXTI9_5_IRQHandler(void){
	if(EXTI_GetITStatus(EXTI_Line7) != RESET){	    //Key1
		TIM_Cmd(TIM2, ENABLE);
		EXTI_ClearITPendingBit(EXTI_Line7); 
	}
	else if(EXTI_GetITStatus(EXTI_Line6) != RESET){	//Key2
		TIM_Cmd(TIM2, ENABLE);
		EXTI_ClearITPendingBit(EXTI_Line6); 
	}
	else if(EXTI_GetITStatus(EXTI_Line8) != RESET){	//Key4
		TIM_Cmd(TIM2, ENABLE);
		EXTI_ClearITPendingBit(EXTI_Line8); 
	}
}	
void EXTI15_10_IRQHandler(void){
	if(EXTI_GetITStatus(EXTI_Line15) != RESET){	    //Key3
		TIM_Cmd(TIM2, ENABLE);
		EXTI_ClearITPendingBit(EXTI_Line15); 
	}
}

/*  原外部按键中断函数
void EXTI9_5_IRQHandler(void){
	if(EXTI_GetITStatus(EXTI_Line7)!=RESET){	//Key1
		//delay_ms(20);
		if(EXTI_GetITStatus(EXTI_Line7)!=RESET){
			//循环选择目标功率
			Power_List_Level++;
			if(Power_List_Level > Power_List_Len-1)   Power_List_Level=0;
			//while(KEY1==0);
			//delay_ms(10);
		}
		EXTI_ClearITPendingBit(EXTI_Line7); 
	}
	else if(EXTI_GetITStatus(EXTI_Line6)!=RESET){	//Key2
		//delay_ms(20);
		if(EXTI_GetITStatus(EXTI_Line6)!=RESET){
            //循环选择目标功率
			
			if(Power_List_Level < 1) Power_List_Level = Power_List_Len;
            Power_List_Level--;
		}
		EXTI_ClearITPendingBit(EXTI_Line6); 
	}

	else if(EXTI_GetITStatus(EXTI_Line8)!=RESET){	//Key4
		//delay_ms(20);
		if(EXTI_GetITStatus(EXTI_Line8)!=RESET){
			//开关机
			if((HX6203.STA==OFF)&&(HX6203.LastSTA==OFF))	    
            {
                HX6203.STA=ON;
            }
			else if((HX6203.STA==ON)&&(HX6203.LastSTA==ON))		
            {
                HX6203.STA=OFF;
            }
		}
		EXTI_ClearITPendingBit(EXTI_Line8); 
	}
}	

//挂起功率设置标志，在main()中UI刷新后赋值装载
void EXTI15_10_IRQHandler(void){
	if(EXTI_GetITStatus(EXTI_Line15)!=RESET){	//Key3
		if(EXTI_GetITStatus(EXTI_Line15)!=RESET){
            
            //if(ACT_Count == 0)
                P_SetFlag = 1;

		}
		EXTI_ClearITPendingBit(EXTI_Line15); 
	}
}
*/
